<?php

namespace app\admin\controller\subject;

use app\common\controller\Backend;

// use traits\model\SoftDelete;

/**
 * 课程购买订单管理
 *
 * @icon fa fa-circle-o
 */
class Order extends Backend
{

    /**
     * Order模型对象
     * @var \app\admin\model\subject\Order
     */
    // use SoftDelete;

    // protected $deleTime = 'deletetime';

    protected $model = null;

     // 关联查询
    protected $relationSearch = true;

    // 初始化
    public function _initialize()
    {
        parent::_initialize();
        // $this->model = new \app\admin\model\subject\Order;
        // $this->view->assign("payList", $this->model->getPayList());


        // 引入模型
        $this->model = model('Subject.Order');
    }

    // 列表
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);

        if ($this->request->isAjax())
        {
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $this->model
                ->with(['subject','business'])
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->count();

            $list = $this->model
                ->with(['subject','business'])
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            // var_dump(collection($list)->toArray());
            // exit;

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }


    // 删除
    public function del($ids = null){

        $list = $this->model->select($ids);

        if(!$list){
            $this->error(__('No Results were found'));
            exit;
        }

        $result = $this->model->destroy($ids);

        if($result === FALSE){
            $this->error($this->model->getError());
            exit;
        }else{
            $this->success();
            exit;
        }
    }

    // 回收站
    public function recyclebin(){
        // 设置过滤方法
        $this->request->filter(['strip_tags']);

        if($this->request->isAjax()){
            if($this->request->request('keyField')){
                return $this->selectpage();
            }

            list($where,$sort,$order, $offset,$limit) = $this->buildparams();

            $total = $this->model
                ->onlyTrashed()
                ->with(['subject','business'])
                ->where($where)
                ->order($sort,$order)
                ->limit($offset,$limit)
                ->count();

            $list = $this->model
                ->onlyTrashed()
                ->with(['subject','business'])
                ->where($where)
                ->order($sort,$order)
                ->limit($offset,$limit)
                ->select();

            $result = array("total" => $total,"rows" => $list);

            return json($result);
        }

    }
}
